// control  layer
var g_controlLayer;
var ControlLayer = cc.Node.extend({
    _hero : 0,
    /////////////////////
    _movingHero : false,
    ctor:function () {
        this._super();
        g_controlLayer=this;
        return true;
    },
    onEnter : function(){
        this._super();
        this.addFightMenu();
        this.addControlMenu();

        this.addKeyboardListener();
    },
    onExit : function(){
        this._super();
    },
    //////////////////////////////////////////////////////
    addFightMenu:function(){
        var filename = MainResource("mAid.png");
        var _aid = new ROL_MenuItemImage(filename,filename,this.onFireEnd , this);
        _aid.setPreCallback( this.onPreAid );
        _aid.setPosition( cc.p( 1210,200 ) );
        filename = MainResource("mFire0.png");
        var filename1 = MainResource("mFire1.png");
        var _fire = new ROL_MenuItemImage(filename,filename1,this.onFireEnd , this);
        _fire.setPreCallback( this.onPreFire );
        _fire.setPosition( cc.p( 1180,90 ) );
        filename = MainResource("mLaser.png");
        var _laser = new ROL_MenuItemImage(filename,filename,this.onFireEnd , this);
        _laser.setPreCallback( this.onPreLaser );
        _laser.setPosition( cc.p( 1070,60 ) );
        var menu = ROL_PreMenu.create( _aid,_fire,_laser );
        menu.setPosition( cc.p(0,0) );
        this.addChild( menu );
    },
    addControlMenu:function(){
        var filename = MainResource("mWheel.png");
        var wheel = new cc.Sprite( filename );
        wheel.setPosition( cc.p( 130,130 ) );
        this.addChild( wheel,261 );
        filename = MainResource("jCenter.png");
        var _center = ROL_MenuItemImage.create( filename, filename,  this.onMoveEnd, this);
        _center.setPosition( cc.p( 130,130 ) );
        filename = MainResource("jDirect.png");
        var _onUp = ROL_MenuItemImage.create( filename, filename,  this.onMoveEnd, this);
        _onUp.setPosition( cc.p( 130,200 ) );
        _onUp.setPreCallback( this.onPreUp );
        var _onDown = ROL_MenuItemImage.create( filename, filename,  this.onMoveEnd, this);
        _onDown.setPosition( cc.p( 130,60 ) );
        _onDown.setPreCallback( this.onPreDown );
        var _onLeft = ROL_MenuItemImage.create( filename, filename,  this.onMoveEnd, this);
        _onLeft.setPosition( cc.p( 60,130 ) );
        _onLeft.setPreCallback( this.onPreLeft );
        var _onRight = ROL_MenuItemImage.create( filename, filename,  this.onMoveEnd, this);        _onRight.setTag(MW.RIGHT);
        _onRight.setPosition( cc.p( 200,130 ) );
        _onRight.setPreCallback( this.onPreRight );
        var menu = ROL_JoyMenu.create( _center,_onUp,_onDown,_onLeft,_onRight );
        menu.setPosition( cc.p(0,0) );
        this.addChild( menu, 260 );
    },
    /////////////////////////////////////
    setControlTarget : function(target){
        this._hero = target;
    },
    /////////////////////////////////////
    onPreAid : function(sender){
        cc.log("*** pre aid");
    },
    onPreLaser : function(sender){
        cc.log("*** pre laser");
    },
    onPreFire : function(sender){
        cc.log("*** pre fire");
        this._hero.firing();
    },
    onFireEnd : function(){
        cc.log("*** on fire");
    },
    /////////////////////////////////////
    onPreUp:function(sender){
        cc.log("*** pre up");
        this._hero.swerve(0);
        this._hero.moveState(true);
    },
    onPreDown:function(sender){
        cc.log("*** pre down");
        this._hero.swerve(2);
         this._hero.moveState(true);
    },
    onPreLeft:function(sender){
        cc.log("*** pre left");
        this._hero.swerve(3);
         this._hero.moveState(true);
    },
    onPreRight:function(sender){
        cc.log("*** pre right");
        this._hero.swerve(1);
         this._hero.moveState(true);
    },
    onMoveEnd:function(sender){
        cc.log("*** on end");
         this._hero.moveState(false);
    },
    /////////////////////////////////////
    addKeyboardListener : function(){
        var that = this;
        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            onKeyPressed:  function(keyCode, event){
                cc.log(keyCode);
                if(keyCode==37||keyCode==65){
                    that.onPreLeft(null);
                }else if(keyCode==38||keyCode==87){
                    that.onPreUp(null);
                }else if(keyCode==39||keyCode==68){
                    that.onPreRight(null);
                }else if(keyCode==40||keyCode==83){
                    that.onPreDown(null);
                }
            },
            onKeyReleased: function(keyCode, event){
                cc.log("release");
                that.onMoveEnd(null);
            }
        }, this);
    }
});
AddControlLayer=function(parent){
    if(parent){
        var layer = new ControlLayer();
        parent.addChild( layer,MW.ORDER.CONTROL );
        cc.log("add ui layer down");
    }else{
        cc.log("invalid parent");
    }
}
RemoveControlLayer=function(clear){
    if(g_controlLayer){
        g_controlLayer.removeFromParent(clear);
        cc.log("remove ui layer down, clear: "+clear);
    }else{
        cc.log("g_controlLayer is invalid");
    }
}
GetControlLayer=function(){
    if(g_controlLayer) {
        cc.log("get  g_controlLayer");
        return g_controlLayer;
    }else{
        cc.log("g_controlLayer invalid");
        return null;
    }
}